package cn.tedu.csmall.passport.preload;

import cn.tedu.csmall.passport.mapper.RoleMappper;
import cn.tedu.csmall.passport.pojo.vo.RoleListItemVO;
import cn.tedu.csmall.passport.repo.RoleRedisRepositoryIml;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * 缓存预热
 */
@Slf4j
//@Component
public class CachePreload implements ApplicationRunner {
    @Autowired
    RoleMappper roleMappper;
    @Autowired
    RoleRedisRepositoryIml repository;

    public CachePreload(){
        log.debug("创建开机自动执行的组件对象: CachePreload");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        repository.deleteAll();
        List<RoleListItemVO> list = roleMappper.list();
        Iterator<RoleListItemVO> iterator = list.iterator();
        while (iterator.hasNext()){
            RoleListItemVO item = iterator.next();
            if (item.getId()==1) {
                iterator.remove();
                break;
            }
        }
        repository.save(list);
    }
}
